﻿Module MODpreferences

    Private fso As Scripting.FileSystemObject
    Private prefsFile As Scripting.TextStream

    Public Function getPref(ByVal relativePath As String, ByVal preference As String)

        On Error Resume Next

        If relativePath <> "" Then relativePath = "\" & relativePath

        With New regOp

            .Root = regOp.RegRoot.HKEY_LOCAL_MACHINE
            .Key = "Software\bbd\AccessSearch\preferences" & relativePath
            getPref = .Value(preference)
        End With

    End Function

    Public Function setPref(ByVal relativePath As String, ByVal preference As String, ByVal Value As Object)

        On Error Resume Next

        If relativePath <> "" Then relativePath = "\" & relativePath

        With New regOp
            .Root = regOp.RegRoot.HKEY_LOCAL_MACHINE
            .Key = "Software\bbd\AccessSearch\preferences" & relativePath
            .Value(preference) = Value
        End With

    End Function

    Public Function saveColumnWidths()

        On Error Resume Next

        strColumnWidths = ""
        Dim i As Integer = 0

        For Each header In FRMmain.DataGrid.Columns
            i = i + 1
            strColumnWidths = strColumnWidths & header.Width & ","
        Next
        If i > 0 Then
            strColumnWidths = Utils.Left(strColumnWidths, Len(strColumnWidths) - 1)
            setPref("", "columnWidths", strColumnWidths)
            DLGpreferences.Hide()
        Else
            MsgBox("There Are Currently No Columns" & vbCrLf & "You Must First Open A Database File")
        End If
    End Function


    Public Sub setDefaultPrefs()

        On Error Resume Next

        With New regOp

            .Root = regOp.RegRoot.HKEY_LOCAL_MACHINE
            .Key = "Software\bbd\AccessSearch"

            If .Value("version") <> "4.2" Then
                .Value("version") = "4.2"
                .Key = "Software\bbd\AccessSearch\preferences"
                .Value("columnWidths") = ""
                .Value("defaultDatabasePath") = Application.ExecutablePath & "\localStock.mdb"
                .Value("defaultSearchField") = "partNumber"
                .Key = "Software\bbd\AccessSearch\preferences\remoteDB"
                .Value("autoUpdate") = 1
                .Value("fieldNames") = "partNumber,quantity,remark,manufacturer,dateCode,userComment"
                .Value("serverAddress") = "example : http://SERVER_ADDRESS"
                .Value("updatePagePath") = "example : FOLDER(s)/PAGE_NAME"
                .Value("databaseName") = "webStock.mdb"
            End If

        End With
    End Sub
    '----------------
    '   NOT USED    '
    '----------------
    'Public Function getPreference(fileName As String, preference As String)
    '    Set fso = New Scripting.FileSystemObject
    '
    '    prefsFilePath = Application.ExecutablePath & "\preferences\" & fileName
    '
    '    If Not fso.FolderExists(Application.ExecutablePath & "\preferences") Then
    '        MsgBox "An error occured while trying to load a preference" & vbCrLf & _
    '            "There is no ""preferences"" folder in the application directory"
    '        Exit Function
    '    End If
    '
    '    If Not fso.FileExists(prefsFilePath) Then
    '        MsgBox "An error occured while trying to load a preference" & vbCrLf & _
    '            "The preference file """ & fileName & """ was not found"
    '        Exit Function
    '    End If
    '
    '    Set prefsFile = fso.OpenTextFile(prefsFilePath, ForReading, False)
    '
    '    Do Until prefsFile.AtEndOfStream
    '        line = Trim(prefsFile.ReadLine)
    '        If Not Left(line, 2) = "//" Then
    '            equalSignPosition = InStr(line, "=")
    '            prefName = Utils.Left(line, equalSignPosition - 1)
    '            If preference = prefName Then
    '                prefValue = Mid(line, equalSignPosition + 1)
    '                getPreference = prefValue
    '            End If
    '        End If
    '    Loop
    '
    'End Function


End Module
